package cn.hxzy.teacher.controller;


import cn.hxzy.common.domain.ClassTable;
import cn.hxzy.common.domain.User;
import cn.hxzy.common.domain.vo.ClassStudentVo;
import cn.hxzy.common.domain.vo.SelectClass;
import cn.hxzy.common.domain.vo.TeacherClassVo;
import cn.hxzy.common.response.R;
import cn.hxzy.common.response.StatusEnum;
import cn.hxzy.teacher.service.TeacherService;
import cn.hxzy.teacher.service.impl.OssService;
import cn.hxzy.common.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

//班级和学生控制器
@RestController
@RequestMapping("/teacher")
public class ClassroomController {

    @Autowired
    private TeacherService teacherService;


    /*
     *  查询班级列表
     **/
    @GetMapping("/selectAllClassList")
    public R<List<ClassStudentVo>> selectAllClassList(@RequestParam(required = true) Integer courseId) {
        List<ClassStudentVo> allClassList = teacherService.selectAllClassList(courseId);
        return R.success(allClassList);
    }


    /**
     * 通过课程id和班级名称  班级创建人  以及是否授权模糊查询按班级列表
     */
    @GetMapping("/selectClassBySearchKey")
    public R<List<ClassStudentVo>> selectClassBySearchKey(
            @RequestParam(required = true) Integer courseId,
            @RequestParam(required = false) String className,
            @RequestParam(required = false) String createName,
            @RequestParam(required = false) Integer isAuthorized) {
        List<ClassStudentVo> classStudentVos = teacherService.selectClassBySearchKey(courseId, className, createName, isAuthorized);
        return R.success(classStudentVos);
    }

    @PostMapping("/addClassGrade")
    public R createClassroomCourseAuthorized(@RequestParam(required = true) String courseId, @RequestParam(required = true) String classId, @RequestParam(required = true) Boolean isAuthorized) {
        return teacherService.createClassroomCourseAuthorized(courseId, classId, isAuthorized);
    }

    /**
     * 创建班级
     *
     * @param clazzName 班级名称
     * @return
     */
    @PostMapping("/createClassroom")
    public R createClassroom(@RequestParam("className") String clazzName) {
        int i = teacherService.selectOneClass(clazzName);
        if (i > 0) {
            return R.error(StatusEnum.PARAM_INVALID);
        }
        User user = ThreadLocalUtil.getUser();
        Integer schoolId = user.getSchoolId();
        Long userId = user.getUserId();

        ClassTable classTable = new ClassTable();
        classTable.setCreateId(Math.toIntExact(userId));
        classTable.setClassName(clazzName);
        classTable.setCreateTime(LocalDateTime.now());
        classTable.setSchoolId(schoolId);
        boolean classroom = teacherService.createClassroom(classTable);
        if (classroom) {
            return R.success("创建班级成功");
        }
        return R.error(StatusEnum.PARAM_INVALID);
    }

    /**
     * 通过教师id查询班级
     */
    /**
     * 通过教师id查询班级
     */
    @GetMapping("/findGradeByTeacherId")
    public R findClassroomByTeacherId() {
        Integer id = Math.toIntExact(ThreadLocalUtil.getUser().getUserId());
        List<TeacherClassVo> teacherClassVos = teacherService.newfindClassroomByTeacherId(id);
        return R.success(teacherClassVos);
    }

    /**
     * 根据班级Id修改班级名称
     *
     * @param classId
     * @param className
     * @return
     */
    @PutMapping("/updateClassroom")
    public R updateClassroom(@RequestParam Integer classId, @RequestParam String className) {
        String s = teacherService.updateClass(classId, className);
        return R.success("修改成功");
    }

    /**
     * 删除班级
     *
     * @param classroomId
     * @return
     */
    @DeleteMapping("deleteClassroom/{classroomId}")
    public R deleteClassroom(@PathVariable Long classroomId) {
        Boolean bool = teacherService.deleteClassroom(classroomId);
        if (bool) {
            return R.success(classroomId);
        } else {
            return R.error(StatusEnum.SERVICE_ERROR);
        }

    }

    /**
     * 模糊查询班级
     *
     * @param className 班级名称
     */
    @GetMapping("/findClassroomBysearch")
    public R<List<SelectClass>> findClassroomByName(@RequestParam(required = false) String className) {
        Integer tId = Math.toIntExact(ThreadLocalUtil.getUser().getUserId());//线程拿取用户id

        if (className != null && !"".equals(className)) {
            List<SelectClass> classroomByName = teacherService.findClassroomByName(className, tId);
            return R.success(classroomByName);
        } else {
            List<SelectClass> list = teacherService.findClassroomByTeacherId(tId);
            return R.success(list);
        }
    }

    @Autowired
    private OssService ossService;


    @PostMapping("/upload")
    public R uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
        String fileName = ossService.uploadFile(file);
        return R.success(fileName);
    }


}



